package com.erp.module.pieceworkGrouping.PieceworkChaxun

import android.content.Context
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.PieceworkcollBean
import com.erp.module.pieceworkGrouping.PieceworkGroupingContract
import com.erp.module.pieceworkGrouping.PieceworkGroupingM
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import org.json.JSONException
import org.json.JSONObject

class PieceworkChaxunActivityP(var V : PieceworkGroupingContract.ProductionChaxunView, var context: Context, var mlist : MutableList<PieceworkcollBean>): BasePullP(), PieceworkGroupingContract.ProductionChaxunKP {
    private var code :Int = 1
    private var starttime : String = ""
    private var endtime : String = ""
    private var batch_names : String = ""
    var md = PieceworkGroupingM()
    init {
        PullV=V
    }
    override fun setCode(code: Int,starttime:String,endtime:String,batch_names:String) {
        this.code = code
        this.starttime = starttime
        this.endtime = endtime
        this.batch_names = batch_names
    }

    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        var pages = page+1;
        md.getGroupingHZList(context,pages,starttime,endtime,code,batch_names,onUserDataInterface)
    }

    override fun jsondata(data: String?) {

        try {
            var jo = JSONObject(data)
            var joo = JsonUtil.getjovolue(jo,"data")
            var js = JsonUtil.getjsvolue(joo,"data")
            for (i in 0 until js.length()) {
                var jodata: JSONObject = js.get(i) as JSONObject
                var bean = PieceworkcollBean()
                bean.gangwei_name = JsonUtil.getvolue(jodata,"gangwei_name")
                bean.content_cat = JsonUtil.getvolue(jodata,"content_cat")
                bean.batch_names = JsonUtil.getvolue(jodata,"batch_name")
                bean.mcount = JsonUtil.getvolue(jodata,"mcount")
                bean.pcode = JsonUtil.getvolue(jodata,"pcode")
                bean.pcoll_at = JsonUtil.getvolue(jodata,"pcoll_at")
                bean.square = JsonUtil.getvolue(jodata,"square")
                var jsnames = JsonUtil.getjsvolue(jodata,"worker_group")
                var worker_group = "";
                for(j in 0 until jsnames.length()){
                    var joname: JSONObject = jsnames.get(j) as JSONObject
                    if(worker_group != ""){
                        worker_group += ","
                    }
                    worker_group += JsonUtil.getvolue(joname, "name")+JsonUtil.getvolue(joname, "coefficient")
                }
                bean.worker_group = worker_group
                bean.xm = JsonUtil.getvolue(jodata,"xm")
                bean.pcount = JsonUtil.getvolue(jodata,"pcount")
                bean.pcoll_at_md = JsonUtil.getvolue(jodata,"pcoll_at_md")
                bean.sum_mcount = JsonUtil.getvolue(jodata,"sum_mcount")
                bean.sum_square = JsonUtil.getvolue(jodata,"sum_square")
                bean.pcoll_at_m = JsonUtil.getvolue(jodata,"pcoll_at_m")
                mlist.add(bean)
            }
        }catch (e : JSONException){
            e.printStackTrace()
            return
        }
    }

    override fun listClean() {
        mlist.clear()
    }

}